import { useEffect } from "react";
import { useReactive } from "ahooks";
import { useModel } from "umi";
import { Pagination, Row, Col } from "antd";
import { Card as SemiCard, List, ButtonGroup, Button } from "@douyinfe/semi-ui";
import { IconEdit, IconRefresh } from "@douyinfe/semi-icons";

import useHelperPersonApplication from "../../helper/useHelperPersonApplication";

import useAllApplications from "../../hooks/application/person/useAllApplications";

import ApplicationDetail from "../../components/person_application/Detail";

const AllPersonApplications = () => {
    const { windowInnerHeight } = useModel("global");

    const { openEditPersonApplicationModal } = useHelperPersonApplication();

    const { loading, totalCount, pageSize, applications, setPage, loadAllApplications } = useAllApplications();

    const state = useReactive({
        selectedApplication: null,
    });

    useEffect(() => {
        let selectedApplication = applications.find((item) => item.appUuid === state.selectedApplication?.appUuid);
        !selectedApplication && applications.length > 0 && (selectedApplication = applications[0]);
        state.selectedApplication = selectedApplication;
    }, [applications]);

    const cardBodyHeight = windowInnerHeight - 90 - 54 - 20;

    return (
        <Row gutter={10} style={{ padding: 10 }}>
            <Col span={5}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用列表 ({totalCount})</span>}
                    headerExtraContent={
                        <ButtonGroup>
                            <Button icon={<IconRefresh />} onClick={loadAllApplications}></Button>
                        </ButtonGroup>
                    }
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 1, height: cardBodyHeight - 46, overflow: "auto" }}
                    footerStyle={{ padding: 0 }}
                    footerLine={true}
                    footer={
                        <div style={{ padding: "11px 0", textAlign: "center" }}>
                            <Pagination
                                simple
                                total={totalCount}
                                pageSize={pageSize}
                                position={["none", "bottomCenter"]}
                                hideOnSinglePage={false}
                                showSizeChanger={false}
                                onChange={(page) => setPage(page)}
                            />
                        </div>
                    }
                >
                    <List
                        loading={loading}
                        dataSource={applications}
                        renderItem={(item, index) => (
                            <List.Item
                                key={index}
                                className={`list-item ${state.selectedApplication?.appUuid === item?.appUuid ? "list-selected-item" : ""}`}
                                main={
                                    <div title={item.appDesc}>
                                        {item?.appName || "-"} <span style={{ fontSize: 12, color: "#999" }}>({item.appRole})</span>
                                    </div>
                                }
                                onClick={() => (state.selectedApplication = item)}
                            ></List.Item>
                        )}
                    />
                </SemiCard>
            </Col>
            <Col span={19}>
                <SemiCard
                    title={<span style={{ lineHeight: "32px", fontSize: 16 }}>应用信息 - {state.selectedApplication?.appName}</span>}
                    headerExtraContent={
                        <ButtonGroup>
                            {state.selectedApplication?.appUuid && (
                                <Button icon={<IconEdit />} onClick={() => openEditPersonApplicationModal(state.selectedApplication, loadAllApplications)}>
                                    编辑应用信息
                                </Button>
                            )}
                        </ButtonGroup>
                    }
                    headerStyle={{ padding: "10px 14px" }}
                    shadows="always"
                    bordered={false}
                    bodyStyle={{ padding: 10, height: cardBodyHeight, overflow: "auto" }}
                >
                    {state.selectedApplication && <ApplicationDetail item={state.selectedApplication || {}} reloadData={loadAllApplications} />}
                </SemiCard>
            </Col>
        </Row>
    );
};

export default AllPersonApplications;
